package de.damontools.awolowa.application;

import greendroid.widget.QuickAction;
import greendroid.widget.QuickActionBar;
import greendroid.widget.QuickActionWidget;

import java.util.ArrayList;

import de.damontools.awolowa.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TableRow;
import android.widget.TextView;

public class ProfileListAdapter extends ArrayAdapter<Profile> {
	private ArrayList<Profile> items;
	private Context context;
	
	/**
	 * Constructor
	 * 
	 * @param context
	 * @param textViewResourceId
	 * @param items
	 */
	public ProfileListAdapter(Context context, int textViewResourceId, ArrayList<Profile> items) {
		super(context, textViewResourceId, items);
		this.context = context;
		this.items = items;
	}
	
	
	/**
	 * Read the activity_profil.xml and create a list of all existing profiles.
	 * 
	 * 
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.activity_profil, null);
		}
		Profile o = items.get(position);
		
		if (o != null) {
			TextView profilname = (TextView) v.findViewById(R.id.profil_name);
			
			TableRow tr = (TableRow) v.findViewById(R.id.profil_row);
			
			tr.setId(o.getID());
			
			// LongClickListener
			if(tr != null) {
				tr.setOnLongClickListener(new OnLongClickListener() {

					public boolean onLongClick(View v) {
						Log.d("awolowa", "TEST");
						QuickActionWidget bar =new QuickActionBar(context);
						bar.addQuickAction(new QuickAction(context,
								R.drawable.gd_action_bar_compose, R.string.gd_compose));
						
						bar.show(v);
						
						return true;
					}
				});
			}

			if (profilname != null) {
				profilname.setText( o.getProfilName());
			}
		}
		return v;
	}
	
}
